package com.crm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.domain.dto.DictDto;
import com.crm.domain.entity.DictData;
import com.crm.domain.vo.OneVO;
import com.crm.domain.vo.RowsVO;
import com.crm.mapper.DictDataMapper;
import com.crm.service.DictDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 15890
 */
@Service
@RequiredArgsConstructor
public class DictDataServiceImpl extends  ServiceImpl<DictDataMapper,DictData> implements DictDataService {
    private final DictDataMapper dictDataMapper;

    @Transactional
    @Override
    public List<DictData> selectAll(String dictType) {
        QueryWrapper<DictData> wrapper = new QueryWrapper<>();
        wrapper.like("dict_type",dictType);
        List<DictData> dictData = dictDataMapper.selectList(wrapper);
        return dictData;
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param dictType
     * @param status
     * @param dictLabel
     * @return
     */
    @Override
    public RowsVO listByPage(Integer pageNum, Integer pageSize, String dictType, Integer status, String dictLabel) {
        Page<DictData> page = new Page<>(pageNum, pageSize);

        // 创建LambdaQueryWrapper并设置条件
        LambdaQueryWrapper<DictData> queryWrapper = new LambdaQueryWrapper<>();
        if (dictType != null && !dictType.isEmpty()) {
            // 假设DictData类中有一个名为dictType的字段
            queryWrapper.like(DictData::getDictType, dictType);
        }
        if (status != null) {
            // 假设DictData类中有一个名为status的字段
            queryWrapper.eq(DictData::getStatus, status);
        }
        if (dictLabel != null && !dictLabel.isEmpty()) {
            // 假设DictData类中有一个名为dictLabel的字段
            queryWrapper.like(DictData::getDictLabel, dictLabel);
        }

        // 使用查询条件分页查询
        Page<DictData> rows = dictDataMapper.selectPage(page, queryWrapper);

        // 封装结果
        RowsVO<DictData> vo = new RowsVO<>();
        List<DictData> records = rows.getRecords();
        vo.setRows(records);
        vo.setTotal(rows.getTotal()); // 如果RowsVO中有total属性，可以添加总记录数
        vo.setMsg("查询成功");
        vo.setCode(200);
        return vo;
    }

    /**
     * 字典数据新增
     * @param dictDto
     * @return
     */
    @Override
    public OneVO addDictData(DictDto dictDto) {
        DictData bean = BeanUtil.toBean(dictDto, DictData.class);
        bean.setUpdateBy("admin");
        bean.setCreateBy("admin");
        bean.setCreateTime(LocalDateTime.now());
        bean.setUpdateTime(LocalDateTime.now());
        bean.setDefaultField(false);
        dictDataMapper.insert(bean);
        OneVO<Object> vo = new OneVO<>();
        vo.setMsg("操作成功");
        vo.setCode(200);
        return vo;
    }



}